package com.ruoyi.web.controller.phonecase;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ImageUrlUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.PcForumPost;
import com.ruoyi.system.service.IPcForumPostService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 论坛帖子Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/phonecase/forumPost")
public class PcForumPostController extends BaseController
{
    @Autowired
    private IPcForumPostService pcForumPostService;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 处理帖子图片URL，转换为完整URL
     */
    private void processPostImages(PcForumPost post)
    {
        if (post != null && StringUtils.isNotEmpty(post.getPostImages()))
        {
            try
            {
                JSONArray imageArray = JSON.parseArray(post.getPostImages());
                String baseUrl = serverConfig.getUrl();
                for (int i = 0; i < imageArray.size(); i++)
                {
                    String imageUrl = imageArray.getString(i);
                    if (StringUtils.isNotEmpty(imageUrl))
                    {
                        imageArray.set(i, ImageUrlUtils.getFullImageUrl(imageUrl, baseUrl));
                    }
                }
                post.setPostImages(imageArray.toJSONString());
            }
            catch (Exception e)
            {
                // JSON 解析失败，忽略
            }
        }
    }

    /**
     * 处理帖子列表图片URL，转换为完整URL
     */
    private void processPostImagesList(List<PcForumPost> list)
    {
        if (list != null && !list.isEmpty())
        {
            String baseUrl = serverConfig.getUrl();
            for (PcForumPost post : list)
            {
                if (StringUtils.isNotEmpty(post.getPostImages()))
                {
                    try
                    {
                        JSONArray imageArray = JSON.parseArray(post.getPostImages());
                        for (int i = 0; i < imageArray.size(); i++)
                        {
                            String imageUrl = imageArray.getString(i);
                            if (StringUtils.isNotEmpty(imageUrl))
                            {
                                imageArray.set(i, ImageUrlUtils.getFullImageUrl(imageUrl, baseUrl));
                            }
                        }
                        post.setPostImages(imageArray.toJSONString());
                    }
                    catch (Exception e)
                    {
                        // JSON 解析失败，忽略
                    }
                }
            }
        }
    }

    /**
     * 查询论坛帖子列表
     */
    @PreAuthorize("@ss.hasPermi('phonecase:forumPost:list')")
    @GetMapping("/list")
    public TableDataInfo list(PcForumPost pcForumPost)
    {
        startPage();
        List<PcForumPost> list = pcForumPostService.selectPcForumPostList(pcForumPost);
        // 处理图片地址，转换为完整URL
        processPostImagesList(list);
        return getDataTable(list);
    }

    /**
     * 导出论坛帖子列表
     */
    @PreAuthorize("@ss.hasPermi('phonecase:forumPost:export')")
    @Log(title = "论坛帖子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcForumPost pcForumPost)
    {
        List<PcForumPost> list = pcForumPostService.selectPcForumPostList(pcForumPost);
        // 处理图片地址，转换为完整URL
        processPostImagesList(list);
        ExcelUtil<PcForumPost> util = new ExcelUtil<PcForumPost>(PcForumPost.class);
        util.exportExcel(response, list, "论坛帖子数据");
    }

    /**
     * 获取论坛帖子详细信息
     */
    @PreAuthorize("@ss.hasPermi('phonecase:forumPost:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") Long postId)
    {
        PcForumPost post = pcForumPostService.selectPcForumPostByPostId(postId);
        // 处理图片地址，转换为完整URL
        processPostImages(post);
        return success(post);
    }

    /**
     * 新增论坛帖子
     */
    @PreAuthorize("@ss.hasPermi('phonecase:forumPost:add')")
    @Log(title = "论坛帖子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PcForumPost pcForumPost)
    {
        pcForumPost.setCreateBy(getUsername());
        return toAjax(pcForumPostService.insertPcForumPost(pcForumPost));
    }

    /**
     * 修改论坛帖子
     */
    @PreAuthorize("@ss.hasPermi('phonecase:forumPost:edit')")
    @Log(title = "论坛帖子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PcForumPost pcForumPost)
    {
        pcForumPost.setUpdateBy(getUsername());
        return toAjax(pcForumPostService.updatePcForumPost(pcForumPost));
    }

    /**
     * 删除论坛帖子
     */
    @PreAuthorize("@ss.hasPermi('phonecase:forumPost:remove')")
    @Log(title = "论坛帖子", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(pcForumPostService.deletePcForumPostByPostIds(postIds));
    }
}

